<template>
  <div class="app-container home">

    <el-row>
      <el-col :sm="24" :lg="12" style="padding-left: 20px">
        <el-form ref="queryParams" :model="queryParams" :inline="true" label-width="80px">
          <el-form-item label="选择日期">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              unlink-panels
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
            ></el-date-picker>
          </el-form-item>
        </el-form>

      </el-col>
      <el-button type="primary" round icon="el-icon-search" @click="handleSearch">查询</el-button>
      <el-button type="primary" round icon="el-icon-search" @click="handleTest">测试</el-button>
    </el-row>
    <!--    <el-divider/>-->
    <el-row :gutter="20">
      <el-col :xs="24" :sm="24" :md="12" :lg="8">
        <el-card class="update-log">
          <div slot="header" class="clearfix">
            <span>消费饼图</span>
          </div>
          <div class="body">
            <div id="pieing" style="height: 350px">
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="8">
        <el-card class="update-log">
          <div slot="header" class="clearfix">
            <span>消费折线图</span>
          </div>
          <div id="lineing" style="height: 350px">
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="8">
        <el-card class="update-log">
          <div slot="header" class="clearfix">
            <span>消费柱状图</span>
          </div>
          <div id="baring" style="height: 350px">
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {pieData, LineData, exportExcel} from "@/api/system/cost";
import moment from "moment";

export default {
  name: "Display",
  data() {
    return {
      // 版本号
      version: "3.8.7",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 1000,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined
      },
      // 总条数
      total: 0,
      pieData: [],
      allCount: 0,
      dateRange: [],
      search: {
        params: {}
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  watch: {},
  created() {
    this.dateRange = this.getMonth();
    this.search.params['beginTime'] = this.dateRange[0];
    this.search.params['endTime'] = this.dateRange[1];
    this.getPieData(this.addDateRange(this.queryParams, this.getMonth()))
    this.getLineData()

  },
  mounted() {
    this.drawLine()
    this.drawBar()

  },
  methods: {
    goTarget(href) {
      window.open(href, "_blank");
    },
    async getPieData(query) {
      await pieData(query).then(response => {
          try {
            if (response.code == '200') {
              this.pieData = response.data.dataItems;
              this.allCount = response.data.total;
              this.drawPie();
            }
          } catch (e) {
            console.log(e)
          }
        }
      );
    },
    async getLineData() {
      await LineData(this.search).then(response => {
          try {
            if (response.code == '200') {
              this.drawLine(response.data.lineDataDate, response.data.lineDataTotal)
              this.drawBar(response.data.lineDataDate, response.data.lineDataTotal)
            }
          } catch (e) {
            console.log(e)
          }

          // this.tableData = response.rows;
          // this.total = response.total;
          // this.loading = false;
        }
      );
      // await pieData(query).then(response => {
      //     try {
      //       if (response.code == '200') {
      //         this.pieData = response.data.dataItems;
      //         this.allCount = response.data.total;
      //         this.drawPie();
      //       }
      //     } catch (e) {
      //       console.log(e)
      //     }
      //   }
      // );
    },
    drawPie() {
      var chartDom = document.getElementById('pieing');
      var myChart = this.echarts.init(chartDom);
      var option;

      option = {
        title: {
          text: '消费统计',
          // subtext: '本月',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        graphic: [
          {
            //环形图中间添加文字
            type: "text", //通过不同top值可以设置上下显示
            left: "center",
            top: "center",
            style: {
              text: `共计\n ${this.allCount}元 `,
              textAlign: "center",
              fill: "#1E7CE8", //文字的颜色
              fontSize: 20,
              lineHeight: 20,
            },
          },
        ],
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            data: this.pieData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      };

      option && myChart.setOption(option);
    },
    drawLine(xData, yData) {
      var chartDom = document.getElementById('lineing');
      var myChart = this.echarts.init(chartDom);
      var option;

      option = {
        tooltip: {
          trigger: 'axis' // 触发类型：坐标轴触发
        },
        xAxis: {
          type: 'category',
          data: xData,
          axisLabel: {
            show: true,
            rotate: 35,
          }
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: yData,
            type: 'line'
          }
        ]
      };

      option && myChart.setOption(option);
    },
    drawBar(xData, yData) {
      var chartDom = document.getElementById('baring');
      var myChart = this.echarts.init(chartDom);
      var option;

      option = {
        tooltip: {
          trigger: 'axis' // 触发类型：坐标轴触发
        },
        xAxis: {
          type: 'category',
          data: xData,
          axisLabel: {
            show: true,
            rotate: 35,
          }
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: yData,
            type: 'bar'
          }
        ]
      };

      option && myChart.setOption(option);
    },
    getMonth() {
      var startMonth = moment().startOf("month");
      var endMonth = moment().endOf("month");
      var dateArr = [
        startMonth.format("YYYY-MM-DD"),
        endMonth.format("YYYY-MM-DD")
      ];
      return dateArr;
    },
    handleSearch() {
      this.search.params['beginTime'] = this.dateRange[0];
      this.search.params['endTime'] = this.dateRange[1];
      let query = this.addDateRange(this.queryParams, this.dateRange)
      this.getPieData(query)
      this.getLineData()

    },
    handleTest() {
      // let query = this.addDateRange(this.queryParams, this.dateRange)
      // let search = {};
      // debugger
      let query = {
        pageNum: 1,
        pageSize: 1000
      }

      this.download('system/cost/export', {
        ...query
      }, `user_${new Date().getTime()}.xlsx`)
      // exportExcel(query).then(response => {
      //
      //   }
      // );
    }
  }
};
</script>

<style scoped lang="scss">
.home {
  blockquote {
    padding: 10px 20px;
    margin: 0 0 20px;
    font-size: 17.5px;
    border-left: 5px solid #eee;
  }

  hr {
    margin-top: 20px;
    margin-bottom: 20px;
    border: 0;
    border-top: 1px solid #eee;
  }

  .col-item {
    margin-bottom: 20px;
  }

  ul {
    padding: 0;
    margin: 0;
  }

  font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 13px;
  color: #676a6c;
  overflow-x: hidden;

  ul {
    list-style-type: none;
  }

  h4 {
    margin-top: 0px;
  }

  h2 {
    margin-top: 10px;
    font-size: 26px;
    font-weight: 100;
  }

  p {
    margin-top: 10px;

    b {
      font-weight: 700;
    }
  }

  .update-log {
    ol {
      display: block;
      list-style-type: decimal;
      margin-block-start: 1em;
      margin-block-end: 1em;
      margin-inline-start: 0;
      margin-inline-end: 0;
      padding-inline-start: 40px;
    }
  }
}
</style>

